package game.module.item.dao;

import game.common.BeanTool;
import game.constants.EffectType;
import game.constants.GameConstant;
import game.constants.SkillId;
import game.module.item.beans.DrugComparator;
import game.module.item.beans.Goodmodel;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.Cache.ValueWrapper;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Component;

@Component
public class GoodModelCache {

	public static final Logger logger = LoggerFactory.getLogger(GoodModelCache.class);

	@Autowired
	private GoodModelDao goodModelDao;

	@Autowired
	private CacheManager cacheManager;

	private GoodModelCache() {
	}

	@SuppressWarnings({ "unchecked" })
	private void initAllGoodModel() {
		try {
			List<Goodmodel> goodmodellist = goodModelDao.getAll();

			Cache cache = cacheManager.getCache(GameConstant.CACHE_GOODS_MODEL);
			ValueWrapper idKeyGoods = cache.get(GameConstant.CACHE_GOODS_MODEL_ID_KEY_GOODS);
			Map<Integer, Goodmodel> idKeyGoodsMap = null;
			if (idKeyGoods != null) {
				idKeyGoodsMap = (Map<Integer, Goodmodel>) idKeyGoods.get();
			} else {
				idKeyGoodsMap = new HashMap<Integer, Goodmodel>();
				cache.put(GameConstant.CACHE_GOODS_MODEL_ID_KEY_GOODS, idKeyGoodsMap);
			}
			BeanTool.addOrUpdate(idKeyGoodsMap, goodmodellist, "id");
			ValueWrapper nameKeyGoods = cache.get(GameConstant.CACHE_GOODS_MODEL_NAME_KEY_GOODS);
			Map<String, Goodmodel> nameKeyGoodsMap = null;
			if (idKeyGoods != null) {
				nameKeyGoodsMap = (Map<String, Goodmodel>) nameKeyGoods.get();
			} else {
				nameKeyGoodsMap = new HashMap<String, Goodmodel>();
				cache.put(GameConstant.CACHE_GOODS_MODEL_NAME_KEY_GOODS, idKeyGoodsMap);
			}
			BeanTool.addOrUpdate(nameKeyGoodsMap, goodmodellist, "name");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}

	/**
	 * 根据物品名字获取物品模型
	 * 
	 * @param name
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Goodmodel getCacheNameGoodModelMapByName(String name) {
		Map<String, Goodmodel> nameKeyGoodsMap = (Map<String, Goodmodel>) cacheManager.getCache(GameConstant.CACHE_GOODS_MODEL).get(GameConstant.CACHE_GOODS_MODEL_NAME_KEY_GOODS)
				.get();
		return nameKeyGoodsMap.get(name);
	}

	@SuppressWarnings("unchecked")
	public Goodmodel get(int id) {
		Cache cache = cacheManager.getCache(GameConstant.CACHE_GOODS_MODEL);
		Map<Integer, Goodmodel> idKeyGoods = (Map<Integer, Goodmodel>) cache.get(GameConstant.CACHE_GOODS_MODEL_ID_KEY_GOODS).get();
		return idKeyGoods.get(id);
	}

	/**
	 * 初始化药物类道具
	 * 
	 * @param goodmodellist
	 */
	@SuppressWarnings("unchecked")
	private void initDrug() {
		Cache cache = cacheManager.getCache(GameConstant.CACHE_GOODS_MODEL);
		Map<Integer, Goodmodel> idKeyGoods = (Map<Integer, Goodmodel>) cache.get(GameConstant.CACHE_GOODS_MODEL_ID_KEY_GOODS).get();
		List<Goodmodel> hpDrugList = new ArrayList<Goodmodel>();// 从大到小排列药片恢复值
		List<Goodmodel> mpDrugList = new ArrayList<Goodmodel>();
		List<Goodmodel> spDrugList = new ArrayList<Goodmodel>();
		List<Goodmodel> hpDrugBuffList = new ArrayList<Goodmodel>();// 从大到小排列hpBuff恢复值
		List<Goodmodel> mpDrugBuffList = new ArrayList<Goodmodel>();
		for (Iterator<Goodmodel> iterator = idKeyGoods.values().iterator(); iterator.hasNext();) {
			Goodmodel goodmodel = iterator.next();
			if (goodmodel.getKind() == 3) {
				if (goodmodel.getDrugBuffId() == SkillId.AUTO_HP_SKILL_BUFF_ID) {
					hpDrugBuffList.add(goodmodel);
				}
				if (goodmodel.getDrugBuffId() == SkillId.AUTO_MP_SKILL_BUFF_ID) {
					mpDrugBuffList.add(goodmodel);
				}
				if (goodmodel.getDrugType() == EffectType.hp) {
					hpDrugList.add(goodmodel);
				} else if (goodmodel.getDrugType() == EffectType.mp) {
					mpDrugList.add(goodmodel);
				} else if (goodmodel.getDrugType() == EffectType.sp) {
					spDrugList.add(goodmodel);
				}
			}
		}
		Collections.sort(hpDrugList, new DrugComparator());
		Collections.sort(mpDrugList, new DrugComparator());
		Collections.sort(spDrugList, new DrugComparator());
		Collections.sort(hpDrugBuffList, new DrugComparator());
		Collections.sort(mpDrugBuffList, new DrugComparator());
		cache.put(GameConstant.CACHE_GOODS_MODEL_hpDrug, hpDrugList);
		cache.put(GameConstant.CACHE_GOODS_MODEL_mpDrug, mpDrugList);
		cache.put(GameConstant.CACHE_GOODS_MODEL_spDrug, spDrugList);
		cache.put(GameConstant.CACHE_GOODS_MODEL_hpDrugBuff, hpDrugBuffList);
		cache.put(GameConstant.CACHE_GOODS_MODEL_mpDrugBuff, mpDrugBuffList);
	}

	@SuppressWarnings("unchecked")
	private void initEquite() {
		Cache cache = cacheManager.getCache(GameConstant.CACHE_GOODS_MODEL);
		Map<Integer, Goodmodel> idKeyGoods = (Map<Integer, Goodmodel>) cache.get(GameConstant.CACHE_GOODS_MODEL_ID_KEY_GOODS).get();
		Map<Integer, List<Goodmodel>> tempEquite = new HashMap<Integer, List<Goodmodel>>();
		for (Goodmodel goodmodel : idKeyGoods.values()) {
			if (goodmodel.getKind() == 2) {
				addGradeEquiteToMap(tempEquite, goodmodel);
			}

			if (goodmodel.isShelizi()) {
				goodmodel.initPosignerskillSheliziMap();
			}
		}
		cache.put(GameConstant.CACHE_GOODS_MODEL_gradeEquite, tempEquite);
	}

	private void addGradeEquiteToMap(Map<Integer, List<Goodmodel>> tempEquite, Goodmodel goodmodel) {
		List<Goodmodel> list = tempEquite.get(goodmodel.getLimitGrade());
		if (list == null) {
			list = new ArrayList<Goodmodel>();
			tempEquite.put(goodmodel.getLimitGrade(), list);
		}
		list.add(goodmodel);
	}

	/**
	 * 初始化物品相关数据
	 */
	public void initGoodData() {
		// GoodspackgeDateManager.getInstance();
		// GiftPacksManager.getInstance().reload();
		// WeddingRingManager.getInstance().reload();
		// UserGoodActionManager.getInstance().reload();
		initEquite();
		initDrug();
	}

	@PostConstruct
	public void reload() {
		try {
			initAllGoodModel();
			initGoodData();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}

	@SuppressWarnings("unchecked")
	public Collection<Goodmodel> getAlLGoodCollection() {
		Cache cache = cacheManager.getCache(GameConstant.CACHE_GOODS_MODEL);
		Map<Integer, Goodmodel> idKeyGoods = (Map<Integer, Goodmodel>) cache.get(GameConstant.CACHE_GOODS_MODEL_ID_KEY_GOODS).get();
		return idKeyGoods.values();
	}

	@SuppressWarnings("unchecked")
	public List<Goodmodel> getHpDrugList() {
		Cache cache = cacheManager.getCache(GameConstant.CACHE_GOODS_MODEL);
		return (List<Goodmodel>) cache.get(GameConstant.CACHE_GOODS_MODEL_hpDrug).get();
	}

	@SuppressWarnings("unchecked")
	public List<Goodmodel> getMpDrugList() {
		Cache cache = cacheManager.getCache(GameConstant.CACHE_GOODS_MODEL);
		return (List<Goodmodel>) cache.get(GameConstant.CACHE_GOODS_MODEL_mpDrug).get();
	}

	@SuppressWarnings("unchecked")
	public List<Goodmodel> getSpDrugList() {
		Cache cache = cacheManager.getCache(GameConstant.CACHE_GOODS_MODEL);
		return (List<Goodmodel>) cache.get(GameConstant.CACHE_GOODS_MODEL_spDrug).get();
	}

	@SuppressWarnings("unchecked")
	public List<Goodmodel> getHpDrugBuffList() {
		Cache cache = cacheManager.getCache(GameConstant.CACHE_GOODS_MODEL);
		return (List<Goodmodel>) cache.get(GameConstant.CACHE_GOODS_MODEL_hpDrugBuff).get();
	}

	@SuppressWarnings("unchecked")
	public List<Goodmodel> getMpDrugBuffList() {
		Cache cache = cacheManager.getCache(GameConstant.CACHE_GOODS_MODEL);
		return (List<Goodmodel>) cache.get(GameConstant.CACHE_GOODS_MODEL_mpDrugBuff).get();
	}

	public List<Goodmodel> getRoushenItemList() {
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<Integer> getEquiteCollectionByGradeAndMenpai(int grade, byte menpai) {
		Cache cache = cacheManager.getCache(GameConstant.CACHE_GOODS_MODEL);
		Map<Integer, List<Goodmodel>> gradeEquite = (Map<Integer, List<Goodmodel>>) cache.get(GameConstant.CACHE_GOODS_MODEL_gradeEquite).get();
		List<Integer> returnlist = new ArrayList<Integer>();
		List<Goodmodel> list = gradeEquite.get(grade);
		if (list == null || list.size() == 0) {
			return returnlist;
		}
		for (Goodmodel gm : list) {
			if (gm.getPosition() <= 12 && gm.getPosition() > 2) {
				if (gm.getPopsinger() == 0 || gm.getPopsinger() == menpai) {
					returnlist.add(gm.getId());
				}
			}
		}
		return returnlist;
	}

	@SuppressWarnings("unchecked")
	public List<Integer> getHorseEquiteCollectionByGradeAndMenpai(int grade) {
		Cache cache = cacheManager.getCache(GameConstant.CACHE_GOODS_MODEL);
		Map<Integer, List<Goodmodel>> gradeEquite = (Map<Integer, List<Goodmodel>>) cache.get(GameConstant.CACHE_GOODS_MODEL_gradeEquite).get();
		List<Integer> returnlist = new ArrayList<Integer>();
		List<Goodmodel> list = gradeEquite.get(grade);
		if (list == null || list.size() == 0) {
			return returnlist;
		}
		for (Goodmodel gm : list) {
			if (gm.getPosition() >= 21 && gm.getPosition() <= 24) {
				returnlist.add(gm.getId());
			}
		}
		return returnlist;
	}

}
